$(function () {
    //注册提交
    function submitData(){
        let mName = $('#mName').val();
        let mPassword = $('#mPassword').val();
        let json = {
            mName: mName,
            mPassword: mPassword,
            method: 'register'
        };
        $.post("/register", json, function (response) {
            let res = JSON.parse(response);
            if (res) {
                $(".msgSuc").show();
                setTimeout(() => {
                    window.location.href = '../html/login.html';
                }, 2000)
            } else {
                $(".msgErr").show();
                setTimeout(() => {
                    $(".msgErr").hide();
                }, 2000)
            }
        });
    }
    //根据校验结果是否发请求
    $("#register").on("click",function(){
        let flag=$("#registerForm").data("bootstrapValidator").isValid();
        if(flag) submitData()
    })
//    表单校验
    $("#registerForm").bootstrapValidator({
        fields: {
            mName: {
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        min: 2,
                        max: 18,
                        message: '用户名长度必须在2到18位之间'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
                        message: '用户名只能包含汉字、字母、数字'
                    }
                }
            },
            mPassword: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    regexp:{
                        regexp:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/,
                        message:'密码必须包括数字和字母'
                    }
                }
            }
        }
    })
//    登录跳转
    $("#submit").on("click", function (e) {
        window.location.href = "../html/login.html";
    })
});